/*===========================================
	Paradigm Engine for Twilight Hack
	Build 1.0
	Scene functions
	
	(C)2008 Vicious1988 - Syndicate Development
	Based off simple rotating cube demo by tkcne
===========================================*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <gccore.h>
#include "c_primitives.h"
#include "textures.h"

extern texobject textures[];
extern int totalcubes,totalspheres,totalpolys,totaltex;
extern primitive cubes[];
extern void draw_quad();

void drawScene(Mtx v){
	int i;
	for (i=0;i<totalcubes;i++){
		float x=cubes[i].x;
		float y=cubes[i].y;
		float z=cubes[i].z;
		float xr=cubes[i].xr;
		float yr=cubes[i].yr;
		float zr=cubes[i].zr;
		float scalex=cubes[i].scalex;
		float scaley=cubes[i].scaley;
		float scalez=cubes[i].scalez;
		float rotx=cubes[i].rotx;
		float roty=cubes[i].roty;
		float rotz=cubes[i].rotz;
		int tex=cubes[i].tex;
		int usetex=cubes[i].usetex;
		
		if (usetex){
			//GX_SetTevOp(GX_TEVSTAGE0, GX_DECAL);
			//GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);

			//GX_SetNumChans(1);
			GX_LoadTexObj(&textures[tex].texObj, GX_TEXMAP0);
		}
		
		s16 cube[] ATTRIBUTE_ALIGN (32) =
		{
			xr-(.5*scalex), yr+(.5*scaley), zr-(.5*scalez),
			xr+(.5*scalex), yr+(.5*scaley), zr-(.5*scalez),
			xr+(.5*scalex), yr+(.5*scaley), zr+(.5*scalez),
			xr-(.5*scalex), yr+(.5*scaley), zr+(.5*scalez),
			xr+(.5*scalex), yr-(.5*scaley), zr-(.5*scalez),
			xr+(.5*scalex), yr-(.5*scaley), zr+(.5*scalez),
			xr-(.5*scalex), yr-(.5*scaley), zr+(.5*scalez),
			xr-(.5*scalex), yr-(.5*scaley), zr-(.5*scalez),
		};
		GX_SetArray (GX_VA_POS, cube, 3 * sizeof (s16));
		
		Mtx m;
		Mtx mv;
		guMtxIdentity (m);
		
		float xrotp=0;
		float yrotp=0;
		float zrotp=0;
		float rotspeed=0;
		
		if ((rotx&&roty)<rotz){
			rotspeed=rotz;
			zrotp=1;
			xrotp=(rotx/rotz);
			yrotp=(roty/rotz);
		}else if ((rotx&&rotz)<roty){
			rotspeed=roty;
			yrotp=1;
			xrotp=rotx/roty;
			zrotp=rotz/roty;
		}else if ((rotz&&roty)<rotx){
			rotspeed=rotx;
			xrotp=1;
			zrotp=rotz/rotx;
			yrotp=roty/rotx;
		}else if ((rotx==roty)&&(roty==rotx)){
			rotspeed=rotx;
			xrotp=1;
			yrotp=1;
			zrotp=1;
		}else if ((rotx==roty)&&(rotx>rotz)){
			rotspeed=rotx;
			xrotp=1;
			yrotp=1;
			zrotp=rotz/rotx;
		}else if ((rotx==rotz)&&(rotx>roty)){
			rotspeed=rotx;
			xrotp=1;
			zrotp=1;
			yrotp=roty/rotx;
		}else if ((rotz==roty)&&(rotz>rotx)){
			rotspeed=rotz;
			zrotp=1;
			yrotp=1;
			xrotp=rotx/rotz;
		}else if ((rotx&&roty&&rotz)==0){
			rotspeed=0;
			xrotp=1;
			yrotp=1;
			zrotp=1;
		}
		
			
		Vector axis = { xrotp, yrotp, zrotp };
		guMtxRotAxisDeg (m, &axis, rotspeed);

		
		//guMtxTransApply (m, m, 0.0F, 0.0F, -200.0F);
		guMtxTransApply (m, m, x, y, z+200.0F);
		guMtxConcat (v, m, mv);
		
		GX_LoadPosMtxImm (mv, GX_PNMTX0);
		GX_Begin (GX_QUADS, GX_VTXFMT0, 24);
			draw_quad (0, 3, 2, 1, 0,usetex);
			draw_quad (0, 7, 6, 3, 1,usetex);
			draw_quad (0, 1, 4, 7, 2,usetex);
			draw_quad (1, 2, 5, 4, 3,usetex);
			draw_quad (2, 3, 6, 5, 4,usetex);
			draw_quad (4, 7, 6, 5, 5,usetex);
		GX_End ();
		GX_DrawDone();
	};
};
